package com.xuxiangyang.competitionsystem.controller;

import com.xuxiangyang.competitionsystem.entities.*;
import com.xuxiangyang.competitionsystem.service.IArticalService;
import com.xuxiangyang.competitionsystem.service.ICompService;
import com.xuxiangyang.competitionsystem.tools.FileUtils;
import com.xuxiangyang.competitionsystem.tools.UUIDTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/competition")
@CrossOrigin
public class CompController {

    @Autowired
    private ICompService compService;

    @Autowired
    private UUIDTools tools;

    @Autowired
    private FileUtils fileUtils;

    //新增
    @RequestMapping("/add")
    public RetResult add(Competition competition, HttpServletRequest request) throws FileNotFoundException {
        competition.setCompId(tools.getUUID());
        if (!competition.getTempUrl().equals("") && competition.getTempUrl() != null) {
//        获取临时文件地址
            String[] split = competition.getTempUrl().split("/");
//        存入模板文件
            String url = fileUtils.downloadFile(request, competition.getTempUrl(),ResourceUtils.getURL("classpath:").getPath(), "static/templates/",split[split.length-1]);
            fileUtils.downloadFile(request, competition.getTempUrl(),"src/main/resources/", "static/templates/",split[split.length-1]);
            competition.setTempUrl(url);
        }
        compService.insert(competition);
        return RetResponse.makeOKRsp();
    }

    //根据用户的学校获取
    @RequestMapping("/getListAll")
    public RetResult getAllList(User user) {
        List<Competition> listAll = compService.getListAll(user);
        return RetResponse.makeOKRsp(listAll);
    }

    //编辑
    @RequestMapping("/edit")
    public RetResult edit(Competition competition, HttpServletRequest request) throws FileNotFoundException {
        if (!competition.getTempUrl().equals("") && competition.getTempUrl() != null) {
            String[] split = competition.getTempUrl().split("/");
//        存入模板文件
            String url = fileUtils.downloadFile(request, competition.getTempUrl(),ResourceUtils.getURL("classpath:").getPath(), "static/templates/",split[split.length-1]);
            fileUtils.downloadFile(request, competition.getTempUrl(),"src/main/resources/", "static/templates/",split[split.length-1]);
            competition.setTempUrl(url);
        }
        compService.updateComp(competition);
        return RetResponse.makeOKRsp();
    }

    //删除
    @RequestMapping("/delete")
    public RetResult delete(Competition competition) {
        compService.deleteById(competition);
        return RetResponse.makeOKRsp();
    }

    //提交报名
    @RequestMapping("submit")
    public RetResult submit(Registration registration) {
        registration.setRegId(tools.getUUID());
        User stuIdByName = compService.getStuIdByName(registration);
        if(stuIdByName == null) {
            return RetResponse.makeErrRsp("报名失败，该学生不存在！");
        }else {
            String userId = stuIdByName.getUserId();
            registration.setUserId(userId);
            compService.submit(registration);
            return RetResponse.makeOKRsp();
        }
    }

    //获根该学校下报名某竞赛但是未添加入组的学生
    @RequestMapping("/getStuNoGroup")
    public RetResult getStuNoGroup(Registration registration) {
        List<Registration> stuNoGroup = compService.getStuNoGroup(registration);
        return RetResponse.makeOKRsp(stuNoGroup);
    }

    //提交参赛小组
    @RequestMapping("/submitGroup")
    public RetResult submitGroup(Group group) {
        group.setGroupId(tools.getUUID());
        for (int i = 0; i < group.getUserIds().length; i++) {
            group.setRegId(group.getUserIds()[i]);
            compService.updateGroupOfReg(group);
        }
        compService.submitGroup(group);
        return RetResponse.makeOKRsp();
    }

    //获取参赛小组
    @RequestMapping("/getGroup")
    public RetResult getGroup(Group group) {
        List<Group> groupList = compService.getGroup(group);
        for (Group g : groupList) {
            g.setRegistrations(compService.getStuByGroup(g));
        }
        return RetResponse.makeOKRsp(groupList);
    }

    //更新小组成员
    @RequestMapping("/updateGroupStu")
    public RetResult updateGroupStu(Group group) {
        List<Registration> stuByGroup = compService.getStuByGroup(group);
        for (Registration student : stuByGroup) {
            compService.updateGroupOfRegNull(student);
        }
        for (int i = 0; i < group.getUserIds().length; i++) {
            group.setRegId(group.getUserIds()[i]);
            compService.updateGroupOfReg(group);
        }
        return RetResponse.makeOKRsp();
    }

    //更新小组信息
    @RequestMapping("/updateGroup")
    public RetResult updateGroup(Group group) {
        compService.updateGroup(group);
        List<Group> groupList = compService.getGroup(group);
        for (Group g : groupList) {
            g.setRegistrations(compService.getStuByGroup(g));
        }
        return RetResponse.makeOKRsp(groupList);
    }

    //获取某用户的竞赛信息
    @RequestMapping("/getCompByUser")
    public RetResult getCompByUser(User user) {
        List<Group> compByUser = compService.getCompByUser(user);
        for (Group g : compByUser) {
            g.setRegistrations(compService.getStuByGroup(g));
        }
        return RetResponse.makeOKRsp(compByUser);
    }

    //改变项目状态
    @RequestMapping("/updateGroupState")
    public RetResult updateGroupState(Group group) {
        compService.updateGroupState(group);
        return RetResponse.makeOKRsp();
    }

    //通过学校获取项目小组
    @RequestMapping("/getGroupBySchool")
    public RetResult getGroupBySchool(User user) {
        List<Group> groupBySchool = compService.getGroupBySchool(user);
        return RetResponse.makeOKRsp(groupBySchool);
    }



}
